# my_importer.py
import sys
import os
import requests
requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS = "TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-256-GCM-SHA384:ECDHE:!COMPLEMENTOFDEFAULT"
password = "fpl6346699"
#判断是否为腾讯云函数环境
yun = os.environ.get('TENCENTCLOUD_RUNENV')
path = "code/import/"
if yun == "SCF":
    path = "/tmp/" + path

def updateFile(name):
    pathName = path + name + ".py"    
    pathDir = pathName[0:pathName.rfind("/")]   
    if yun != "SCF":
        if os.path.exists(pathName) == True:
            return pathDir
    url = "https://gitee.com/fpanlong/lianTong/raw/master/code/import/" + name + ".py"
    html = requests.get(url)
    if html.status_code == 200:
        text = html.text
    else:
        url = "https://gitee.com/fpanlong/lianTong/raw/master/code/" + name + ".py"
        text = requests.get(url).text   
    if os.path.exists(pathDir) == False:
        os.makedirs(pathDir)        
    
    if os.path.exists(pathName): 
        os.remove(pathName) 
        
    with open(pathName,mode="w+") as f:
        f.write(text)
    print("模块已更新:" + name)
    return  pathDir            
def myImport(name):
    pathDir = updateFile(name)
    if pathDir not in sys.path:
        sys.path.append(pathDir)    
